package com.wbao.dianniu.ui;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.sdk.android.media.upload.UploadListener;
import com.alibaba.sdk.android.media.upload.UploadTask;
import com.alibaba.sdk.android.media.utils.FailReason;
import com.alibaba.sdk.android.media.utils.StringUtils;
import com.arlen.photo.photopickup.presenter.HeadPresenter;
import com.arlen.photo.photopickup.util.DensityUtils;
import com.arlen.photo.photopickup.util.FileSizeUtil;
import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.OptionsPickerView.OnOptionsSelectListener;
import com.bumptech.glide.Glide;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.customView.CustomDialog;
import com.wbao.dianniu.customView.LoadingWaitDialog;
import com.wbao.dianniu.data.ChildZone;
import com.wbao.dianniu.data.HeadInfoData;
import com.wbao.dianniu.data.Zone;
import com.wbao.dianniu.listener.IAccountModifyListener;
import com.wbao.dianniu.listener.IHeadInfoListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.AccountModifyManager;
import com.wbao.dianniu.manager.HeadImageManger;
import com.wbao.dianniu.manager.HeadInfoManager;
import com.wbao.dianniu.utils.GlideCircleTransform;
import com.wbao.dianniu.utils.JsonUtil;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 资料
 * 
 * @author yejianjun
 * 
 */
public class PerfectInfoActivity extends BaseActivity implements
		OnClickListener, IHeadInfoListener, IAccountModifyListener {
	private final int INDUSTRY_CODE = 1000;
	private final int PLATFORM_CODE = 2000;
	private final int JOB_CODE = 3000;
	private static final int NICE_REQ_CODE = 10010;
	private static final int SIGNATE_REQ_CODE = 10011;
	private static final int SEX_REQ_CODE = 10013;
	private final int IMAGE_OPEN = 1; // 打开图片标记
	private final int TAKE_PHOTO = 2; // 打开图片标记
	
	private LinearLayout waittingLayout;
	private ProgressBar waittingProBar;
	private TextView waittingTV;
	private RelativeLayout headRL;
	private ImageView headIV;
	private RelativeLayout nicknameRL; 
	private TextView nicknameTV;
	private RelativeLayout sexRL;
	private TextView sexTV;
	private RelativeLayout signateRL;
	private TextView signateTV;
	private Dialog dialog;
	private Button photoBtn;
	private Button choosePicBtn;
	private Button cancel_dialog;
	private AccountModifyManager modifyManger;
	private RelativeLayout industryRL;
	private RelativeLayout jobsRL;
	private RelativeLayout platformRL;
	private RelativeLayout regionRL;
	private TextView industryTV;
	private TextView jobsTV;
	private TextView platformTV;
	private TextView areaTV;
	private Button rightBtn;
//	int selectFlag = -1;
	/** 省列表 */
	private LinkedList<ChildZone> province_list = new LinkedList<ChildZone>();
	/** 市列表 */
	private ArrayList<LinkedList<ChildZone>> city_list = new ArrayList<>();
	/** 省数据 */
	private ArrayList<String> provinceList = new ArrayList<>();
	/** 市数据 */
	private ArrayList<ArrayList<String>> cityList = new ArrayList<>();
	private OptionsPickerView pvOptions;
	private int provinceId = 1;
	private int cityId = 1;
	private String province;
	private String city;
	private String headBitmap = null;
	private String headPic = null;
	private Dialog pDialog;
//	private boolean isChangeFlag = false;
	private HeadInfoData historyData = null;
	private boolean isHeadPicChange  = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_perfect_info,
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		setTitleName(getResources().getString(R.string.edit_information));
		initWattingView();
		// 请求数据
		requestData(GlobalContext.accountId,GlobalContext.accountId);
		initView();
		rightBtn = getRightTextButton();
		rightBtn.setText(getResources().getString(R.string.save));
		rightBtn.setOnClickListener(this);
		initAreaData();
	}

	private void initWattingView(){
		waittingLayout = (LinearLayout) findViewById(R.id.waitting_layout);
		waittingProBar = (ProgressBar) findViewById(R.id.waitting_progress);
		waittingTV = (TextView) findViewById(R.id.waitting_text_desc);
	}
	
	private void initView() {
		headRL = (RelativeLayout) findViewById(R.id.perfect_head_rl);
		nicknameRL = (RelativeLayout) findViewById(R.id.perfect_nickname_rl);
		sexRL = (RelativeLayout) findViewById(R.id.perfect_sex_rl);
		signateRL = (RelativeLayout) findViewById(R.id.perfect_signate_rl);
		industryRL = (RelativeLayout) findViewById(R.id.perfect_industry_rl);
		jobsRL = (RelativeLayout) findViewById(R.id.perfect_jobs_rl);
		platformRL = (RelativeLayout) findViewById(R.id.perfect_platform_rl);
		regionRL = (RelativeLayout) findViewById(R.id.perfect_region_rl);
		headRL.setOnClickListener(this);
		nicknameRL.setOnClickListener(this);
		sexRL.setOnClickListener(this);
		signateRL.setOnClickListener(this);
		industryRL.setOnClickListener(this);
		jobsRL.setOnClickListener(this);
		platformRL.setOnClickListener(this);
		regionRL.setOnClickListener(this);

		headIV = (ImageView) findViewById(R.id.perfect_head_iv);
		PictureDownloadUtils.displayHeadImage(this,headIV, GlobalContext.headPic);
		nicknameTV = (TextView) findViewById(R.id.perfect_nickname_tv);
		sexTV = (TextView) findViewById(R.id.perfect_sex_tv);
		signateTV = (TextView) findViewById(R.id.perfect_signate_tv);
		industryTV = (TextView) findViewById(R.id.perfect_industry_tv);
		jobsTV = (TextView) findViewById(R.id.perfect_jobs_tv);
		platformTV = (TextView) findViewById(R.id.perfect_platform_tv);
		areaTV = (TextView) findViewById(R.id.perfect_area_tv);
	}
	/**
	 * 地区选择器
	 */
	private void initAreaData(){
		//选项选择器
	    pvOptions = new OptionsPickerView(this);
		InputStream input = getResources().openRawResource(R.raw.zone);
		String jsonData = Utils.inputStream2String(input, "utf-8");
		List<Zone> zoneList = (List<Zone>) JsonUtil.toObjectList(jsonData, Zone.class);
		for (Iterator iterator = zoneList.iterator(); iterator.hasNext();) {
			Zone zone = (Zone) iterator.next();
			ChildZone child = new ChildZone();
			child.setId(zone.getId());
			child.setName(zone.getName());
			province_list.add(child);
			provinceList.add(zone.getName());
			LinkedList<ChildZone> linkList = zone.getChildrens();
			city_list.add(linkList);
			ArrayList<String> arrayList = new ArrayList<String>();
			for (int i = 0; i < linkList.size(); i++) {
				arrayList.add(linkList.get(i).getName());
			}
			cityList.add(arrayList);
		}
		
	    pvOptions.setPicker(provinceList, cityList, true);
        pvOptions.setTitle(getResources().getString(R.string.city_selected));
        pvOptions.setCyclic(false);
		
        pvOptions.setSelectOptions(provinceId, cityId);
        pvOptions.setOnoptionsSelectListener(new OnOptionsSelectListener() {

			@Override
			public void onOptionsSelect(int options1, int option2, int options3) {
				provinceId = options1;
				cityId = option2;
				province = provinceList.get(options1);
				city = cityList.get(options1).get(option2);
				String areaStr = provinceList.get(options1) + cityList.get(options1).get(option2) ;
				areaTV.setText(areaStr);
			}
		});
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_button:
			if(isNoChanged()){
				finish();
				break;
			}
			CustomDialog.Builder builder = new CustomDialog.Builder(this);
			builder.setTitle("确认放弃本次资料编辑吗？");
			builder.setPositiveButton(getResources().getString(R.string.confirm),
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							finish();
						}
					});
			builder.setNegativeButton(getResources().getString(R.string.cancel),
					new android.content.DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
			builder.create().show();
			break;
		case R.id.right_button:	//保存
			if(checkNull()){
				Notification.toast(this, "必填项不能为空");
				return;
			}else{
				int sex = -1;
				String sexStr = sexTV.getText().toString().trim();
				if(sexStr.equals("女")){
					sex = 0;
				}else if(sexStr.equals("男")){
					sex = 1;
				}
				modifyManger.modify(GlobalContext.accountId, headPic, nicknameTV.getText().toString().trim(),sex , signateTV.getText().toString().trim(), industryTV.getText().toString().trim(), jobsTV.getText().toString().trim(), platformTV.getText().toString().trim(), province, city);
				// 提示等待框
				
			}
			break;
		case R.id.perfect_head_rl:	//修改头像
			showRealase();
			break;
		case R.id.perfect_nickname_rl:	//编辑昵称
			Intent niceIntent = new Intent();
			niceIntent.putExtra(GlobalContext.INTENT_NICENAME, nicknameTV.getText().toString().trim());
			niceIntent.setClass(PerfectInfoActivity.this, NiceNameEdit.class);
			startActivityForResult(niceIntent, NICE_REQ_CODE);
			break;
		case R.id.perfect_sex_rl:
			Intent sexIntent = new Intent();
			String sexText = sexTV.getText().toString().trim();
			int sexFlag = -1;
			if(!TextUtils.isEmpty(sexText)){
				if(sexText.equals("男")){
					sexFlag = 1;
				}else if(sexText.equals("女")){
					sexFlag = 0;
				}
			}
			sexIntent.putExtra(GlobalContext.INTENT_SEX, sexFlag);
			sexIntent.setClass(PerfectInfoActivity.this, SexEdit.class);
			startActivityForResult(sexIntent, SEX_REQ_CODE);
			break;
		case R.id.perfect_signate_rl:
			Intent signateIntent = new Intent();
			signateIntent.putExtra(GlobalContext.INTENT_SIGNATE, signateTV.getText().toString().trim());
			signateIntent.setClass(PerfectInfoActivity.this, SignateEdit.class);
			startActivityForResult(signateIntent, SIGNATE_REQ_CODE);
			break;
		
		case R.id.perfect_industry_rl: // 行业
			Intent industryIntent = new Intent();
			industryIntent.putExtra("industry", industryTV.getText().toString().trim());
			industryIntent.setClass(PerfectInfoActivity.this, TradeListActivity.class);
			startActivityForResult(industryIntent, INDUSTRY_CODE);
			break;
		case R.id.perfect_jobs_rl:	// 岗位
			Intent jobIntent = new Intent();
			jobIntent.putExtra("jobSelect", jobsTV.getText().toString().trim());
			jobIntent.setClass(PerfectInfoActivity.this, JobSelectActivity.class);
			startActivityForResult(jobIntent, JOB_CODE);
			break;
		case R.id.perfect_platform_rl:
			Intent platformIntent = new Intent();
			platformIntent.putExtra("platform", platformTV.getText().toString().trim());
			platformIntent.setClass(PerfectInfoActivity.this, PlatformListActivity.class);
			startActivityForResult(platformIntent, PLATFORM_CODE);
			break;
		case R.id.perfect_region_rl:
			 pvOptions.show();
			break;
		case R.id.btn_cancel:
			if (null != dialog && dialog.isShowing()) {
				dialog.dismiss();
			}
			break;
		case R.id.btn_take_photo:
			if (null != dialog && dialog.isShowing()) {
				dialog.dismiss();
			}
			//拍照
			 Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			 takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"temp.jpg")));
			 startActivityForResult(takeIntent, TAKE_PHOTO);
			break;
		case R.id.btn_pick_photo:
			if (null != dialog && dialog.isShowing()) {
				dialog.dismiss();
			}
			//选择图片
			Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(intent, IMAGE_OPEN);
			break;
		default:
			break;
		}

	}
	
	/*private void showAuthDialog(String msg) {
		dialog = new Dialog(this, R.style.custom_Dialog);
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(3, 3, 3, 3);
		win.setGravity(Gravity.CENTER);
		View view = LayoutInflater.from(this).inflate(R.layout.custom_message_dialog, null);
		Button okBtn = (Button) view.findViewById(R.id.custom_msg_ok);
		Button cancelBtn = (Button) view.findViewById(R.id.custom_msg_cancel);
		TextView message = (TextView) view
				.findViewById(R.id.custom_msg_message);
		message.setText(msg);
		dialog.setContentView(view);
		dialog.setCancelable(true);
		dialog.show();
		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		});
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		});
		
	}*/

	// 请求数据
	private void requestData(int accountId,int friendId) {
		HeadInfoManager manager = new HeadInfoManager(this);
		manager.addHeadListener(this);
		manager.headinfo(accountId, friendId);
		modifyManger = new AccountModifyManager(this);
		modifyManger.addAccountModifyListener(this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		super.onActivityResult(requestCode, resultCode, data);
		//打开图片  
        if(resultCode==RESULT_OK && requestCode==IMAGE_OPEN) {        
            Uri uri = data.getData();  
            if (!TextUtils.isEmpty(uri.getAuthority())) {  
                //查询选择图片  
                Cursor cursor = getContentResolver().query( uri,   new String[] { MediaStore.Images.Media.DATA },  
                        null, null, null);  
                //返回 没找到选择图片  
                if (null == cursor) {
                    return;  
                }  
                //光标移动至开头 获取图片路径  
                cursor.moveToFirst();  
                String pathImage = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); 
                cursor.close(); 
                Intent pIntent = new Intent();
                pIntent.setClass(this, CutImageActivity.class);
                pIntent.putExtra(GlobalContext.BUNDLE_DATA, pathImage);
                startActivityForResult(pIntent, 110);
                
                /*pDialog = new ProgressDialog(this);
                pDialog.setCancelable(false);
    			pDialog.show();
                //图片选择成功，上传图片到cdn，得到cdn的路径
                HeadPresenter presenter = new HeadPresenter(PerfectInfoActivity.this);
                presenter.addUploadListener(uploadListener);
                headPic = "image_" + StringUtils.getUUID();
                presenter.uploadImageUrl( pathImage, headPic);
                headBitmap = FileSizeUtil.compressBitmap(pathImage, headIV.getWidth(), headIV.getHeight());
    			headIV.setImageBitmap(headBitmap);
    			isHeadPicChange = true;*/
            }
        } else if(resultCode==RESULT_OK && requestCode==TAKE_PHOTO){
        	 File file = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
        	 // 照片先缓存到本地 
        	 BitmapFactory.Options options = FileSizeUtil.getBitmapOptions(file.getPath());
             int screenMax = Math.max(DensityUtils.getWindowWidth(this),
                     DensityUtils.getWindowHeight(this));
             int imgMax = Math.max(options.outWidth, options.outHeight);
             int inSimpleSize = 1;
             if (screenMax <= imgMax) {
                 inSimpleSize = Math.max(screenMax, imgMax) / Math.min(screenMax, imgMax);
             }
          String   cachPath = "image_" + StringUtils.getUUID();
             // 缓存图片到本地
             String compressPath = FileSizeUtil.compressBitmap(this,
                     file.getAbsolutePath(),
                     Bitmap.CompressFormat.JPEG,
                     options.outWidth / inSimpleSize,
                     options.outHeight / inSimpleSize,
                     false,cachPath);
        	 Intent pIntent = new Intent();
             pIntent.setClass(this, CutImageActivity.class);
             pIntent.putExtra(GlobalContext.BUNDLE_DATA, compressPath);
             startActivityForResult(pIntent, 110);
			/* //图片选择成功，上传图片到cdn，得到cdn的路径
        	 HeadPresenter presenter = new HeadPresenter(PerfectInfoActivity.this);
             presenter.addUploadListener(uploadListener);
            headPic = "image_" + StringUtils.getUUID();
            presenter.uploadImageUrl(picture.getAbsolutePath(), headPic);
            headBitmap = FileSizeUtil.compressBitmap(picture.getPath(), headIV.getWidth(), headIV.getHeight());
            headIV.setImageBitmap(headBitmap);
            isHeadPicChange = true;*/
        }else if(resultCode == RESULT_OK && requestCode == INDUSTRY_CODE){
        	String name = data.getStringExtra(GlobalContext.INTENT_MORE_TRADE);
			if(!TextUtils.isEmpty(name)){
				industryTV.setText(name);
			}else{
				industryTV.setText("");
			}
        }else if(resultCode == RESULT_OK && requestCode == PLATFORM_CODE){
        	String name = data.getStringExtra(GlobalContext.INTENT_MORE_TRADE);
			if(!TextUtils.isEmpty(name)){
				platformTV.setText(name);
			}else{
				platformTV.setText("");
			}
        }else if(resultCode == RESULT_OK && requestCode == JOB_CODE){
        	String name = data.getStringExtra(GlobalContext.INTENT_MORE_TRADE);
			if(!TextUtils.isEmpty(name)){
				jobsTV.setText(name);
			}else{
				jobsTV.setText("");
			}
        }else if(resultCode == RESULT_OK && requestCode == NICE_REQ_CODE){
        	String name = data.getStringExtra(GlobalContext.NICE_NAME_INTENT);
			if(!TextUtils.isEmpty(name)){
				nicknameTV.setText(name);
			}else{
				nicknameTV.setText("");
			}
        }else if(resultCode == RESULT_OK && requestCode == SEX_REQ_CODE){
        	int sex = data.getIntExtra(GlobalContext.INTENT_SEX , -1);
			if(0 == sex){
				sexTV.setText("女");
			}else if(1 == sex){
				sexTV.setText("男");
			}else{
				sexTV.setText("");
			}
        }else if(resultCode == RESULT_OK && requestCode == SIGNATE_REQ_CODE){
        	String name = data.getStringExtra(GlobalContext.INTENT_SIGNATE);
			if(!TextUtils.isEmpty(name)){
				signateTV.setText(name);
			}else{
				signateTV.setText("");
			}
        }else if(requestCode == 110 && resultCode == RESULT_OK ){
        	String cachePath = data.getStringExtra("cachePath");
        	String uploadName = data.getStringExtra("headPic");
        	System.out.println("cachePath:" + cachePath);
        	System.out.println("uploadName:" + uploadName);
        	if(null != cachePath && null != uploadName){
        		headPic = uploadName;
                 pDialog = LoadingWaitDialog.createLoadingDialog(this, "");
                 pDialog.setCancelable(false);
     			 pDialog.show();
                 //图片选择成功，上传图片到cdn，得到cdn的路径
                 HeadPresenter presenter = new HeadPresenter(PerfectInfoActivity.this);
                 presenter.addUploadListener(uploadListener);
                 presenter.uploadImageNotCompress(cachePath, uploadName);
//                 headBitmap = FileSizeUtil.compressBitmap(cachePath, headIV.getWidth(), headIV.getHeight());
                  //BitmapFactory.decodeFile(cachePath);
//     			headIV.setImageBitmap(headBitmap);
                 headBitmap = cachePath;
				Glide.with(this)
						.load(new File(cachePath))
						.centerCrop()
						.placeholder(R.drawable.default_head_icon)
						.crossFade()
						.override(Utils.dip2px(this, 40),Utils.dip2px(this, 40))
						.bitmapTransform(new GlideCircleTransform(this))
						.thumbnail(0.1f)
						.into(headIV);
     			isHeadPicChange = true;
        	}
        }
	}
	
	
	
	@Override
	public void onHeadFailure(int errorCode, String errorMsg) {
		waittingLayout.setVisibility(View.VISIBLE);
		waittingProBar.setVisibility(View.GONE);
		waittingTV.setText("加载失败，请稍后再试...");
	}
	
	@Override
	public void onHeadSuccess(HeadInfoData objdata) {
		waittingLayout.setVisibility(View.GONE);
		if(null != objdata){
			historyData = objdata;
			if(null == objdata.getHeadPic()){
				//为空，默认加载一张图片
				headIV.setImageResource(R.drawable.default_head_icon);
			}else{
				PictureDownloadUtils.displayHeadImage(this,headIV, objdata.getHeadPic());
//				HeadPresenter presenter = new HeadPresenter(this);
//				presenter.showHeadPic(this, objdata.getHeadPic(), headIV);
			}
			headPic = objdata.getHeadPic();
			nicknameTV.setText(objdata.getRealName() == null ? "":objdata.getRealName());
			if(null == objdata.getSex()){
				sexTV.setText("");
			} else {
				float sexInt = Float.valueOf(objdata.getSex());
				if (0 == sexInt) {
					sexTV.setText("女");
				} else if (1 == sexInt) {
					sexTV.setText("男");
				}
			}
			signateTV.setText(objdata.getDescribe() == null ? "":objdata.getDescribe());
			industryTV.setText(objdata.getIndustry() == null ? "":objdata.getIndustry());
			jobsTV.setText(objdata.getPost() == null ? "":objdata.getPost());
			platformTV.setText(objdata.getPlatform() == null ? "":objdata.getPlatform());
			province = objdata.getProvince();
			city = objdata.getCity();
			if(TextUtils.isEmpty(province)){
				province = "";
			}
			if(TextUtils.isEmpty(city)){
				city = "";
			}
			areaTV.setText(province + city);
		}
	}

	/**
	 * 选择图片弹出框
	 */
	private void showRealase() {
		dialog = new Dialog(this, R.style.custom_Dialog);
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(0, 0, 0, 0);
		win.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
		WindowManager.LayoutParams lp = win.getAttributes();
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		win.setAttributes(lp);
		win.setWindowAnimations(R.style.buttom_animation);
		View view = LayoutInflater.from(this).inflate(R.layout.picture_select,null);
		photoBtn = (Button) view.findViewById(R.id.btn_take_photo);
		choosePicBtn = (Button) view.findViewById(R.id.btn_pick_photo);
		cancel_dialog = (Button) view.findViewById(R.id.btn_cancel);
		photoBtn.setOnClickListener(this);
		choosePicBtn.setOnClickListener(this);
		cancel_dialog.setOnClickListener(this);
		dialog.setContentView(view);
		dialog.setCancelable(true);
		dialog.show();
	}

	private boolean checkNull(){
		if(TextUtils.isEmpty(nicknameTV.getText().toString().trim())){
			return true;
		}else if(TextUtils.isEmpty(sexTV.getText().toString().trim())){
			return true;
		}else if(TextUtils.isEmpty(industryTV.getText().toString().trim())){
			return true;
		}else if(TextUtils.isEmpty(jobsTV.getText().toString().trim())){
			return true;
		}else if(TextUtils.isEmpty(platformTV.getText().toString().trim())){
			return true;
		}else if(TextUtils.isEmpty(areaTV.getText().toString().trim())){
			return true;
		}
		return false;
	}

	@Override
	public void onModifyFailure(int errorCode, String errorMsg) {
		
	}

	@Override
	public void onModifySuccess() {
		GlobalContext.isPerfectData = true;
		if(null != headBitmap){
			HeadImageManger.getInstance().changeImageData(headBitmap);
			GlobalContext.headPic = headPic;
		}
		String label = industryTV.getText().toString().trim()+"/"+jobsTV.getText().toString().trim()+"/"+platformTV.getText().toString().trim()+"|"+areaTV.getText().toString().trim();
		HeadImageManger.getInstance().changeDatas(nicknameTV.getText().toString().trim(), sexTV.getText().toString().trim(), signateTV.getText().toString().trim(), label);
		Notification.toast(this,"已保存");
		finish();
	}
	
	/**
	 * 上传图片事件监听
	 */
	UploadListener uploadListener = new UploadListener() {

		@Override
		public void onUploading(UploadTask uploadTask) {
		}

		@Override
		public void onUploadFailed(UploadTask arg0, FailReason arg1) {
			// TODO Auto-generated method stub
			if(null != pDialog && pDialog.isShowing()){
				pDialog.dismiss();
			}
			Notification.toast(PerfectInfoActivity.this, "上传失败");
		}

		@Override
		public void onUploadComplete(UploadTask arg0) {
			if(null != pDialog && pDialog.isShowing()){
				pDialog.dismiss();
			}
		}

		@Override
		public void onUploadCancelled(UploadTask arg0) {
			if(null != pDialog && pDialog.isShowing()){
				pDialog.dismiss();
			}
		}
	};
	
	protected void onDestroy() {
		super.onDestroy();
		if(null != pDialog && pDialog.isShowing()){
			pDialog.dismiss();
		}
	};
	
	private boolean isNoChanged(){
		if(null == historyData)
			return true;
		int sex = -1;
		String sexStr = sexTV.getText().toString().trim();
		if(sexStr.equals("女")){
			sex = 0;
		}else if(sexStr.equals("男")){
			sex = 1;
		}
		if(TextUtils.isEmpty(historyData.getRealName())){
			historyData.setRealName("");
		}
		if(null == historyData.getSex()){
			historyData.setSex(-1);
		}
		if(TextUtils.isEmpty(historyData.getDescribe())){
			historyData.setDescribe("");
		}
		if(TextUtils.isEmpty(historyData.getIndustry())){
			historyData.setIndustry("");
		}
		if(TextUtils.isEmpty(historyData.getPost())){
			historyData.setPost("");
		}
		if(TextUtils.isEmpty(historyData.getPlatform())){
			historyData.setPlatform("");
		}
		if(TextUtils.isEmpty(historyData.getProvince())){
			historyData.setProvince("");
		}
		if(TextUtils.isEmpty(historyData.getCity())){
			historyData.setCity("");
		}
		if(historyData.getRealName().equals(nicknameTV.getText().toString().trim()) &&
				historyData.getSex() == sex &&
				historyData.getDescribe().equals(signateTV.getText().toString().trim()) &&
				historyData.getIndustry().equals(industryTV.getText().toString().trim()) && 
				historyData.getPost().equals(jobsTV.getText().toString().trim()) &&
				historyData.getPlatform().equals(platformTV.getText().toString().trim()) &&
				historyData.getProvince().equals(province) && 
				historyData.getCity().equals(city) && !isHeadPicChange
				){
			return true;
		}else{
			return false;
		}
			
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
		
}
